package com.estate.struts.actions;

import java.sql.Date;
import java.util.List;

import oracle.sql.DATE;

import com.estate.business.DocumentService;
import com.estate.business.LendDocumentService;
import com.estate.utils.LendingAuthorStateUtil;
import com.estate.vo.impl.DocumentVo;
import com.estate.vo.impl.LendDocumentVo;

public class PatronAction extends EstateSupport {

	private LendDocumentService lendDocumentService;
	private DocumentService documentService;
	private String method;
	private String state;
	private String multiType;
	private String multiDelete;
	private String delStr;
	private String deleteId;
	private String lendId;
	private String receiveId;

	public String execute() throws Exception {

		String page = "";
		if ("list".equals(method)) {
			List<LendDocumentVo> vo = lendDocumentService.findByState(Integer
					.parseInt(state));
			for(int i=0;i<vo.size();i++){
				vo.get(i).setStateName(LendingAuthorStateUtil.getAuthorStateString(vo.get(i).getAuthorState()));
			}
			this.getSession().setAttribute("lendDocument", vo);
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			
			this.getSession().setAttribute(
					"stateName",
					LendingAuthorStateUtil.getAuthorStateString(Integer
							.parseInt(state)));
			page = "list";
		} else if ("delete".equals(method)) {
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!" + delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						lendDocumentService.deleteByPK(Integer
								.parseInt(delArray[i]));
					}
				}
			} else {
				lendDocumentService.deleteByPK(Integer.parseInt(this
						.getDeleteId()));
			}

			page = "refresh";
		} else if("lend".equals(method)){
			System.out.print("in lend");
			LendDocumentVo vo=new LendDocumentVo();
			vo.setAuthorState(2);
			Date date=new Date(System.currentTimeMillis());
			vo.setApplyDate(date);
			vo.setDocument((DocumentVo)documentService.findByPK(Integer.parseInt(this.getLendId())));
			vo.setUser(this.getSessionUser());
			vo.getDocument().setLendState(2);
			documentService.update(vo.getDocument());
			lendDocumentService.addLendDocumentVo(vo);
			System.out.print("out lend");
			page="lend";
		} else if("receive".equals(method)){
			DocumentVo vo=(DocumentVo)documentService.findByPK(Integer.parseInt(this.getReceiveId()));
			LendDocumentVo lendVo=lendDocumentService.findVoByDocumentId(Integer.parseInt(this.getReceiveId()));
			Date date=new Date(System.currentTimeMillis());
			lendVo.setReturnDate(date);
			lendVo.setAuthorState(1);
			vo.setLendState(1);
			documentService.update(vo);
			lendDocumentService.update(lendVo);
			page="lend";
		}
		return page;
	}

	public void setLendDocumentService(LendDocumentService lendDocumentService) {
		this.lendDocumentService = lendDocumentService;
	}

	public LendDocumentService getLendDocumentService() {
		return lendDocumentService;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getState() {
		return state;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setLendId(String lendId) {
		this.lendId = lendId;
	}

	public String getLendId() {
		return lendId;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}

	public void setReceiveId(String receiveId) {
		this.receiveId = receiveId;
	}

	public String getReceiveId() {
		return receiveId;
	}
}
